package com.jztd.servone.invoice.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jztd.platfrom.base.exception.BusinessException;
import com.jztd.platfrom.base.exception.ResourceNotFoundException;
import com.jztd.platfrom.base.util.UserUtil;
import com.jztd.servone.emun.InvoicePartyCodeEnum;
import com.jztd.servone.invoice.dto.OrderDTO;
import com.jztd.servone.emun.ClientTypeEnum;
import com.jztd.servone.invoice.param.OrderInterfaceConfigParam;
import com.jztd.servone.invoice.param.OrderParam;
import com.jztd.servone.invoice.po.BusinessConfig;
import com.jztd.servone.invoice.po.Merchants;
import com.jztd.servone.invoice.service.BusinessConfigService;
import com.jztd.servone.invoice.service.MerchantsService;
import com.jztd.servone.invoice.service.OrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderServiceImpl implements OrderService {
    private final BusinessConfigService businessConfigService;
    private final MerchantsService merchantsService;
    @Override
    public OrderDTO getOrderInfo(BusinessConfig businessConfig,OrderParam param) {
        if (ObjectUtil.isEmpty(param.getInvoiceCodes())) {
            throw new ResourceNotFoundException("暂无订单信息");
        }
        OrderInterfaceConfigParam interfaceConfigParam = businessConfig.getOrdeDetailsrUrl();
        String url = businessConfig.getUrl();
        if (ObjectUtil.isEmpty(interfaceConfigParam)) {
        }
        if (ObjectUtil.isNotEmpty(interfaceConfigParam.getMethod())) {
            url += interfaceConfigParam.getMethod();
        }
        String result = null;
        if (ObjectUtil.isNotEmpty(interfaceConfigParam.getRequestType()) && interfaceConfigParam.getRequestType().equals("POST")) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("ticketId", param.getInvoiceCodes());
            paramMap.put("merchantNo", UserUtil.getTenant());
            result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        } else {
            result = HttpUtil.get(url + "?ticketId=" + param.getInvoiceCodes()+"&merchantNo="+UserUtil.getTenant());
        }
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (ObjectUtil.isEmpty(jsonObject.getStr("data"))) {
            return null;
        }
        List<OrderDTO> orderList = Convert.toList(OrderDTO.class, jsonObject.get("data"));
        if(ObjectUtil.isNotEmpty(orderList)){
            OrderDTO orderDTO = orderList.get(0);
            if(ObjectUtil.isEmpty(orderDTO)){
                throw new BusinessException("商家暂不支持线上开票，辛苦联系客服处理");
            }
            return orderDTO;
        }
        return null;
    }

    @Override
    public List<OrderDTO> getInvoiceOrderList(BusinessConfig businessConfig,OrderParam param) {
        OrderInterfaceConfigParam interfaceConfigParam = businessConfig.getOrdeListUrl();
        String url = businessConfig.getUrl();
        if (ObjectUtil.isEmpty(interfaceConfigParam)) {
        }
        if (ObjectUtil.isNotEmpty(interfaceConfigParam.getMethod())) {
            url += interfaceConfigParam.getMethod();
        }
        Merchants merchants = merchantsService.getMerchants(UserUtil.getTenant(), param.getBusinessType());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userId", param.getUserId());
        paramMap.put("businessType", param.getBusinessType());
        paramMap.put("merchantNo", merchants.getMerchantNo());
        paramMap.put("phone", param.getPhone());
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramMap));
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (ObjectUtil.isEmpty(jsonObject.getStr("data"))) {
            return new ArrayList<>();
        }
        List<OrderDTO> orderDTO = Convert.toList(OrderDTO.class, jsonObject.get("data"));
        return orderDTO;
    }

    @Override
    public void updateOrderInfo(BusinessConfig businessConfig,String invoiceCode,String invoiceStatus) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("ticketId", invoiceCode);
        paramMap.put("merchantNo", UserUtil.getTenant());
        paramMap.put("invoiceStatus", invoiceStatus);
        String result = HttpUtil.post(businessConfig.getUrl()+"updateInvoiceStatus", JSONUtil.toJsonStr(paramMap));
    }


    @Override
    public String getServiceType() {
        return ClientTypeEnum.JZTD.name();
    }
}
